﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EquipmentScript : MonoBehaviour {
    public Transform ChoosableEquipmentPanel;

	// Update is called once per frame
	void Update () {
        GameObject.Find("OutfitButton").transform.Find("Image").GetComponent<Image>().sprite = SingletonManager.GetSprite(PlayerInventory.equippedOutfit);
        GameObject.Find("OutfitButton").transform.Find("Image").GetComponent<Image>().preserveAspect = true;
        GameObject.Find("OutfitArmorLabel").GetComponent<Text>().text = PlayerInventory.equippedOutfit.armor + " armor";
        GameObject.Find("WeaponButton").transform.Find("Image").GetComponent<Image>().sprite = SingletonManager.GetSprite(PlayerInventory.equippedWeapon);
        GameObject.Find("WeaponButton").transform.Find("Image").GetComponent<Image>().preserveAspect = true;
        GameObject.Find("WeaponDamageLabel").GetComponent<Text>().text = PlayerInventory.equippedWeapon.damage + " damage";
    }

    public void ChangeOutfit()
    {
        GameObject panel = GameObject.Find("Canvas").transform.Find("EquipPanel").gameObject;
        panel.SetActive(true);
        panel.transform.Find("EquipLabel").GetComponent<Text>().text = "Change equipped outfit";

        Transform subpanel = panel.transform.Find("ChoosableEquipment");
        foreach (Transform child in subpanel)
        {
            Destroy(child.gameObject);
        }

        foreach (Outfit o in PlayerInventory.ownedOutfits)
        {
            Transform spawned = Instantiate(ChoosableEquipmentPanel, subpanel);
            spawned.Find("Image").GetComponent<Image>().sprite = SingletonManager.GetSprite(o);
            spawned.Find("Image").GetComponent<Image>().preserveAspect = true;
            spawned.Find("Text").GetComponent<Text>().text = o.armor + " armor";
            if (o.armor > PlayerInventory.equippedOutfit.armor) spawned.Find("Text").GetComponent<Text>().color = Color.green;
            else if (o.armor < PlayerInventory.equippedOutfit.armor) spawned.Find("Text").GetComponent<Text>().color = Color.red;
            spawned.GetComponent<Button>().onClick.AddListener(() => ChangeOutfitFinally(o.key));
        }
    }

    private void ChangeOutfitFinally(string outfit_key)
    {
        GameObject.Find("Canvas").transform.Find("EquipPanel").gameObject.SetActive(false);
        PlayerInventory.equippedOutfit = SingletonManager.FindOutfit(outfit_key);
        PlayerPrefs.SetString("equippedOutfit", outfit_key);
    }

    public void ChangeWeapon()
    {
        GameObject panel = GameObject.Find("Canvas").transform.Find("EquipPanel").gameObject;
        panel.SetActive(true);
        panel.transform.Find("EquipLabel").GetComponent<Text>().text = "Change equipped weapon";

        Transform subpanel = panel.transform.Find("ChoosableEquipment");
        foreach (Transform child in subpanel)
        {
            Destroy(child.gameObject);
        }

        foreach (Weapon w in PlayerInventory.ownedWeapons)
        {
            Transform spawned = Instantiate(ChoosableEquipmentPanel, subpanel);
            spawned.Find("Image").GetComponent<Image>().sprite = SingletonManager.GetSprite(w);
            spawned.Find("Image").GetComponent<Image>().preserveAspect = true;
            spawned.Find("Text").GetComponent<Text>().text = w.damage + " damage";
            if (w.damage > PlayerInventory.equippedWeapon.damage) spawned.Find("Text").GetComponent<Text>().color = Color.green;
            else if (w.damage < PlayerInventory.equippedWeapon.damage) spawned.Find("Text").GetComponent<Text>().color = Color.red;
            spawned.GetComponent<Button>().onClick.AddListener(() => ChangeWeaponFinally(w.key));
        }
    }

    private void ChangeWeaponFinally(string weapon_key)
    {
        GameObject.Find("Canvas").transform.Find("EquipPanel").gameObject.SetActive(false);
        PlayerInventory.equippedWeapon = SingletonManager.FindWeapon(weapon_key);
        PlayerPrefs.SetString("equippedWeapon", weapon_key);
    }
}
